<?php

/**
 * commentaire actions.
 *
 * @package    explications_informatiques
 * @subpackage commentaire
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class commentaireActions extends sfActions
{
  

 
  public function executeCreate(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_COMS"),"headers","forbidden");
    $this->forwardUnless($request->isMethod(sfRequest::POST),"headers","forbidden");

    $this->form = new CommentairesForm();

    $this->processForm($request, $this->form);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("MODERATION_COMS"),"headers","forbidden");
    $this->forward404Unless($commentaires = Doctrine_Core::getTable('Commentaires')->find(array($request->getParameter('idcommentaires'))), sprintf('Object commentaires does not exist (%s).', $request->getParameter('idcommentaires')));
    $this->form = new CommentairesForm($commentaires);
  }

  public function executeUpdate(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("MODERATION_COMS"),"headers","forbidden");
    $this->forwardUnless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT),"headers","forbidden");
    $this->forward404Unless($commentaires = Doctrine_Core::getTable('Commentaires')->find(array($request->getParameter('idcommentaires'))), sprintf('Object commentaires does not exist (%s).', $request->getParameter('idcommentaires')));
    $this->form = new CommentairesForm($commentaires);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {

	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("MODERATION_COMS"),"headers","forbidden");
    $this->forward404Unless($commentaires = Doctrine_Core::getTable('Commentaires')->find(array($request->getParameter('id'))), sprintf('Object commentaires does not exist (%s).', $request->getParameter('id')));
    $commentaires->getSfGuardUserProfile()->decrPoint(40, "Suppression d'un commentaire");
	$commentaires->delete();
	if ($cache = $this->getContext()->getViewCacheManager())
	{
	   $cache->remove('article/index');
	   $cache->remove('headers/modepanel');
	   $cache->remove('article/show?id_asso='.$request->getParameter('id2'));
	}
	//Log
	  sfCoreAutoload::logElem("Suppression d'un com - id_asso:".$request->getParameter('id2')." coms:".$request->getParameter('id'), $this->getUser()->getGuardUser()->getId(), date("Y-m-d h:i:s"));
	$this->getUser()->setFlash("info","Commentaire supprimé");
    $this->redirect('article/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_COMS"),"headers","forbidden");
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid() && 
		(!$this->getUser()->hasAttribute("com_timming") || intval($this->getUser()->getAttribute("com_timming") + 120 < time())))// 2 min entre chaque com
    {
      $commentaires = new Commentaires();
	  $commentaires->setIdAuteur($this->getUser()->getGuardUser()->getId());
	  $commentaires->setIdAsso($request["commentaire"]["id_asso"]);
	  $commentaires->setTexte($request["commentaire"]["texte"]);
	  $commentaires->setDateAjout(date("Ymdhis"));
	  $commentaires->setOnline(1);
	  $commentaires->replace();
	  if ($cache = $this->getContext()->getViewCacheManager())
		  {
			 $cache->remove('article/index');
			 $cache->remove('headers/modepanel');
			 $cache->remove('article/show?id_asso='.$request["commentaire"]["id_asso"]);
		  }
	  $this->getUser()->setAttribute("com_timming", time());
	  $this->getUser()->setFlash("info","Commentaire ajouté!");
	  $q = Doctrine_Query::create()
		->from('assoplatform n')
		->leftJoin('n.Articles')
		->where('n.idassoplatform = ?',$request["commentaire"]["id_asso"]);
	  $art = $q->execute();
	  $this->getUser()->updatePoint(20, "Ajout d'un commentaire");
	  $this->redirect('@show_article?title=article&idasso='.$request["commentaire"]["id_asso"]);
      
    }
	elseif($this->getUser()->hasAttribute("com_timming"))
	{
		$this->getUser()->setFlash("err","Vous devez attendre 2 mins entre chaque commentaire");
		$this->redirect('@show_article?title=article&idasso='.$request["commentaire"]["id_asso"]);
	}
	
  }
}
